//
//  VerificationCodeView.m
//  WuJieLive
//
//  Created by iMac on 2017/10/19.
//  Copyright © 2017年 zhk. All rights reserved.
//

#import "VerificationCodeView.h"

@implementation VerificationCodeView


-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}

- (void)setupView
{
    
    self.bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    self.bgView.userInteractionEnabled = YES;
    self.bgView.backgroundColor = [UIColor blackColor];
    self.bgView.alpha = 0.5;
    [self addSubview:self.bgView];
    
    self.whiteView = [[UIView alloc]init];
    self.whiteView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.whiteView];
    [self.whiteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(30);
        make.right.offset(-30);
        make.top.offset(100);
        make.height.offset(300);
    }];
    self.whiteView.layer.masksToBounds = YES;
    self.whiteView.layer.cornerRadius = 20;
    
    
    self.closeBtn = [[UIButton alloc]init];
    [self.whiteView addSubview:self.closeBtn];
    [self.closeBtn addTarget:self action:@selector(removeViews:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.closeBtn setBackgroundImage:Image(@"icon_nav_关闭") forState:(UIControlStateNormal)];
    [self.closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-15);
        make.top.offset(15);
        make.size.mas_offset(CGSizeMake(20, 20));
    }];
    
    self.detailLabel = [[UILabel alloc]init];
    self.detailLabel.text = @"请输入下方图片中的数字";
    self.detailLabel.font = FontSize(15);
    self.detailLabel.textColor = TextMainColor;
    [self.whiteView addSubview:self.detailLabel];
    [self.detailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(33);
        make.centerX.equalTo(self.whiteView);
    }];
    
    self.codeImageView = [[UIImageView alloc]init];
    [self.whiteView addSubview:self.codeImageView];
    [self.codeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.detailLabel.mas_bottom).offset(20);
        make.left.offset(55);
        make.right.offset(-55);
        make.height.offset(110);
    }];
    
    self.changeBtn = [[UIButton alloc]init];
    [self.changeBtn setTitle:@"看不清，换一张" forState:(UIControlStateNormal)];
    [self.whiteView addSubview:self.changeBtn];
    self.changeBtn.titleLabel.font = FontSize(12);
    [self.changeBtn setTitleColor:TextLessImportColor forState:(UIControlStateNormal)];
    [self.changeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.whiteView);
        make.top.equalTo(self.codeImageView.mas_bottom).offset(15);
    }];
    
    self.codeText = [[UITextField alloc]init];
    self.codeText.layer.borderColor = TextLessImportColor.CGColor;
    self.codeText.layer.borderWidth = 1.0f;
    [self.whiteView addSubview:self.codeText];
    self.codeText.textAlignment = NSTextAlignmentCenter;
    [self.codeText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.changeBtn.mas_bottom).offset(20);
        make.centerX.equalTo(self.whiteView);
        make.left.offset(30);
        make.right.offset(-30);
        make.height.offset(30);
    }];
    
    self.codeText.returnKeyType = UIReturnKeyDone;
    self.codeText.delegate = self;
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField.text.length == 6) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(GetCode:)]) {
            [self.delegate GetCode:textField.text];
        }
        return YES;
    }else
    {
        [MBProgressHUD showError:@"请检查验证码(6位)"];
        return NO;
    }
}


- (void)removeViews:(UIButton *)sender
{
    [sender removeFromSuperview];
    [self removeFromSuperview];
}


@end
